from wtforms import Form, StringField, PasswordField
from wtforms.validators import Email, DataRequired, Length, ValidationError

from apps.models.user import User


class LoginForm(Form):
    email = StringField(validators=[Email(message='请输入正确格式的邮箱！')])
    password = PasswordField(validators=[Length(6, 18, message='请输入6到18位长度的密码！')])


class RegisterForm(LoginForm):
    nickname = StringField(validators=[DataRequired(message='请输入昵称！')])

    def validate_nickname(self, field):
        if User.query.filter_by(nickname=field.data).first():
            raise ValidationError('该昵称已被注册！')

    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('该邮箱已被注册！')
